package com.busi.init;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class DictionaryConfigXml extends HttpServlet {
			
		private final String xmlPath = "/WEB-INF/dictionary-config.xml";
		
		private static Element rootEle;
		
		private static DictionaryConfigXml dictionaryConfigXml;
		

		
		public static DictionaryConfigXml getInstance(){
			if(dictionaryConfigXml == null){
				dictionaryConfigXml = new DictionaryConfigXml();
			}
			return dictionaryConfigXml;
		}
		
		ServletConfig config;

		public ServletConfig getConfig() {
			return config;
		}
		public void setConfig(ServletConfig config) {
			this.config = config;
		}
			
			
		public void init(ServletConfig conf)throws ServletException{
			 Map dicMap = new HashMap();
			 this.config=conf;
		     String strXmlPath = conf.getServletContext().getRealPath("/") + xmlPath;
		     SAXBuilder sax = new SAXBuilder();
		     try{
		    	 Document doc = sax.build(strXmlPath);
		    	 DictionaryConfigXml.getInstance().rootEle = doc.getRootElement();
		    	 List<Element> listEle = DictionaryConfigXml.getInstance().rootEle.getChildren();
		    	 for(Element e : listEle){
		    		String id = e.getAttribute("id").getValue();
		       		if(e.getAttribute("valid").getValue().equals("1")){
		       			List list = new ArrayList();
		    			List<Element> listSql = e.getChildren("obj");
		    			for(Element esql : listSql){
		    				Map map = new HashMap();
		    				map.put("id", esql.getAttribute("value").getValue());
		    				map.put("value", esql.getTextTrim());
		    				list.add(map);
		    			}
		    			dicMap.put(id, list);
		    		}
		    	 }
		     }catch(Exception e){
		    	 e.printStackTrace();
		     }
		     conf.getServletContext().setAttribute("dicMap", dicMap);
		  }
			
			
		public List getListById(String id){
			List list = new ArrayList();
		    try{
		    	List<Element> listEle = DictionaryConfigXml.getInstance().rootEle.getChildren();
		    	for(Element e : listEle){
		       		if(e.getAttribute("id").getValue().equals(id) && e.getAttribute("value").equals("1")){
		    			List<Element> listSql = e.getChildren("obj");
		    			for(Element esql : listSql){
		    				Map map = new HashMap();
		    				map.put("id", esql.getAttribute("value").getValue());
		    				map.put("value", esql.getTextTrim());
		    				list.add(map);
		    			}
		    		}
		    	}
		    }catch(Exception e){
		    	e.printStackTrace();
		    }
		    return list;
		}
		
		
		public static void main(String[] args) throws Exception{
			DictionaryConfigXml x = DictionaryConfigXml.getInstance();
			String path = "D:\\myobject\\contract_mng\\contract_mng\\WebContent\\WEB-INF\\dictionary-config.xml";
			SAXBuilder sax = new SAXBuilder();
	   	 	Document doc = sax.build(path);
	   	 	x.rootEle = doc.getRootElement();
			x.getListById("company_type");
		}
}
